<?php

namespace App\Console\Commands;


use App\Common\ElasticSearch\DrawLuckLog;
use App\Common\RabbitMQ\AddVideoByTagConsumer;
use App\Common\RabbitMQ\TestConsumer1;
use App\Http\Controllers\v1\algorithmAndDataStruct\AlgorithmController;
use App\Http\Controllers\v1\reflection\Student;
use App\Models\LuckDrawLog;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;


/**
 * test
 */
class Test extends Command
{
    /**
     * 守护进程
     * The name and signature of the console command.
     * 执行命令 ： php artisan test exec --process_num=2 --current_num=1
     * exec 执行选项 process （进程） exec(执行)
     * process_num 开启进程数量
     * current_num 当前第几个
     * @var string
     */

    protected $signature = 'test {option} {--process_num=} {--current_num=}';#你的方法名字


    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '测试';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 多进程
     */
    private function process($process_num)
    {
        for ($i = 0; $i < $process_num; $i++) {
            $shell = vsprintf('php artisan Test exec --process_num %s --current_num %s >> /var/log/crontab_log/Test_%s_$(date "+%%Y%%m%%d").log', [$process_num, $i, $i]) . ' &';
            pclose(popen($shell, 'r'));
            $this->output('启动进程成功_%s, %s', [$process_num, $shell]);
        }
        die;
    }


    /**
     * @return int|void
     * @throws \ReflectionException
     */
    public function handle()
    {
        $a = 1;
        $b = &$a;
        $reflectionClass = new \ReflectionClass(Student::class);
        $studentClass = $reflectionClass->newInstanceArgs(['itianyu',27]);
        $reflectionMethod = $reflectionClass->getMethod('setBase');
        dump($reflectionMethod);//setBase
        $reflectionParameter = $reflectionMethod->getParameters();
        dump($reflectionParameter);//[Printer name year ]
        foreach ($reflectionParameter as $k => $parameter) {
            dump($parameter->getName());// Printer name year
            if ($parameter->getClass()) {
                dump($parameter->getClass()->getName());//Printer
            }
            if ($parameter->isDefaultValueAvailable()) {
                dump($parameter->getDefaultValue());//10
            }
        }
        $reflectionFunction = new \ReflectionFunction('display');
        dump($reflectionFunction);
        dd();
        //批量加数据 luck_draw_log表
        $counter = 5000;
        $start = 11115001;
        while ($counter > 0) {
            $data = [];
            for ($i = $start; $i < $start+5000; $i++) {
                $data[] = [
                    'luck_id' => $i,
                    'draw_id' => $i,
                    'user_id' => $i,
                    'luck_name' => '抽奖券名-'.$i,
                    'draw_name' => '奖品名-'.$i,
                    'user_name' => '用户名-'.$i,
                    'draw_time' => date('Y-m-d H:i:s'),
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
            }
            $start = $i;
            $res = LuckDrawLog::insert($data);
            Log::info('insert result : ' .$res);
            $counter--;
            if ($res === false) {
                Log::info('insert false : ' .$start.'---'.$counter);
            }
        }
        dd((new AlgorithmController())->changeTowNumber(1,2));
        //[2023-03-17 22:00:10]
        $a = (object) [
            '12345jfkd',
            [1,2,3],
            'test' => [1,2,3],
            'testa' => 1
        ];
        foreach ($a as $v) {
            dump($v[0]);
        }
        dd();

        Log::info('test');
        $option = $this->argument('option');
        switch ($option) {
            case 'process':
                $process_num = $this->option('process_num');
                $this->process($process_num);
                break;
            case 'exec':
                $this->exec();
                break;
        }
    }


    /**
     * 执行
     * @return void
     */
//    protected function execute(InputInterface $input, OutputInterface $output)
//    {
//
//        $consumer = new AddVideoByTagConsumer(true);
//        $qu = $consumer->getQueue();
//        $qu->consume(function (\AMQPEnvelope $envelope, \AMQPQueue $queue) {
//            //调用消费逻辑
//            $res = $envelope->getBody();
//            Log::info('从mq中获取消息---'.$res);
//            if ($res) {
//                $queue->ack($envelope->getDeliveryTag());
//            }else{
//                $queue->nack($envelope->getDeliveryTag(),AMQP_REQUEUE);
//            }
//            sleep(3);
//        });
//    }

}
